package com.zj.study.handle;

import com.zj.study.handle.execption.RespException;
import com.zj.study.pojo.FinalResultVO;
import com.zj.study.pojo.RespStatusEnum;
import java.util.Objects;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.ResponseEntity;

@Log4j2
public class RespExceptionHandler implements ExceptionHandler {

  @Override
  public boolean match(Class<?> clazz) {
    return RespException.class.isAssignableFrom(clazz);
  }

  @Override
  public ResponseEntity<?> handler(Throwable throwable) {
    RespException exception = (RespException) throwable;
    RespStatusEnum respStatusEnum = Objects.requireNonNull(exception.getRespStatusEnum());

    if (log.isDebugEnabled()) {
      log.debug("RespException Handler; code:{}, msg:{}, track:{}",
          respStatusEnum.getCode(),
          respStatusEnum.getMsg(),
          exception.getStackTrace());
    }

    return response(FinalResultVO.fail(respStatusEnum, exception.getFireMessage()));
  }
}